package com.jrzh.mvc.controller.sys.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jrzh.framework.I18nHelper;
import com.jrzh.framework.annotation.RequiredPermission;
import com.jrzh.framework.annotation.UserEvent;
import com.jrzh.framework.base.controller.BaseAdminController;
import com.jrzh.framework.bean.JqDataGrid;
import com.jrzh.framework.bean.ResultBean;
import com.jrzh.mvc.constants.SysActionConstants;
import com.jrzh.mvc.convert.sys.FileRuleConvert;
import com.jrzh.mvc.model.sys.FileRuleModel;
import com.jrzh.mvc.search.sys.FileRuleSearch;
import com.jrzh.mvc.service.sys.manage.SysServiceManage;
import com.jrzh.mvc.view.sys.FileRuleView;

@RestController(FileRuleController.LOCATION +"/FileRuleController")
@RequestMapping(FileRuleController.LOCATION)
public class FileRuleController extends BaseAdminController{
	public static final String LOCATION = "sys/admin/fileRule";
	
	@Autowired
	private SysServiceManage sysServiceManage;
	
	@PostMapping(value = "datagrid")
	@UserEvent(desc = "FileRule列表")
	@RequiredPermission(key =  SysActionConstants.INDEX)
	public JqDataGrid<FileRuleView> datagrid(FileRuleSearch search) {
		JqDataGrid<FileRuleView> dg = new JqDataGrid<FileRuleView>();
	    try{
	    	dg = sysServiceManage.fileRuleService.datagrid(search);
	    } catch (Exception e){
	    	e.printStackTrace();
	    }
		return dg;
	}
	
	@PostMapping(value = "add")
	@UserEvent(desc = "FileRule新增")
	@RequiredPermission(key =  SysActionConstants.ADD)
	public ResultBean add(FileRuleView view,BindingResult errors){
		ResultBean result = new ResultBean();
		try{
			FileRuleModel model =new FileRuleConvert().addConvert(view);
			sysServiceManage.fileRuleService.add(model, getSessionUser());
			result.setStatus(ResultBean.SUCCESS);
			result.setMsg(I18nHelper.getI18nByKey("message.addTo_success", getSessionUser()));
		}catch (Exception ex) {
			ex.printStackTrace();
			result.setMsg(ex.getMessage());
		}
		return result;	
	}


	
	@PostMapping(value = "edit/{id}")
	@UserEvent(desc = "FileRule编辑")
	@RequiredPermission(key =  SysActionConstants.EDIT)
	public ResultBean edit(@PathVariable("id") String id, FileRuleView view) {
		ResultBean result = new ResultBean();
		try {
			FileRuleModel model = sysServiceManage.fileRuleService.findById(id);
			model = new FileRuleConvert().editConvert(view, model);
			sysServiceManage.fileRuleService.edit(model, getSessionUser());
			result.setStatus(ResultBean.SUCCESS);
			result.setMsg(I18nHelper.getI18nByKey("message.modify_success", getSessionUser()));
		}catch (Exception ex) {
			ex.printStackTrace();
			result.setMsg(ex.getMessage());
		}
		return result;
	}
	
	@PostMapping(value = "delete/{id}")
	@UserEvent(desc = "FileRule删除")
	@RequiredPermission(key =  SysActionConstants.DELETE)
	public ResultBean delete(@PathVariable("id") String id) {
		ResultBean result = new ResultBean();
		try {
			FileRuleModel model = sysServiceManage.fileRuleService.findById(id);
			sysServiceManage.fileRuleService.delete(model.getId(), getSessionUser());
			result.setStatus(ResultBean.SUCCESS);
			result.setMsg(I18nHelper.getI18nByKey("message.delete_success", getSessionUser()));
		} catch (Exception ex) {
			ex.printStackTrace();
			result.setMsg(ex.getMessage());
		}
		return result;
	}
	
	@PostMapping(value = "changeStatus/{id}")
	@UserEvent(desc = "FileRule启用/禁用")
	@RequiredPermission(key =  SysActionConstants.DISABLE_ENABLE)
	public ResultBean changeStatus(@PathVariable("id") String id){
		ResultBean result = new ResultBean();
		try {
			FileRuleModel model = sysServiceManage.fileRuleService.findById(id);
			model.setIsDisable(!model.getIsDisable());
			sysServiceManage.fileRuleService.edit(model, getSessionUser());
			result.setStatus(ResultBean.SUCCESS);
			result.setMsg(I18nHelper.getI18nByKey("message.modify_success", getSessionUser()));
		} catch (Exception e) {
			e.printStackTrace();
			result.setMsg(e.getMessage());
		}
		return result;
	}
	
	@Override
	protected void setData() {
	}

}
